﻿using Candy.Infrastructure;
using Candy.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Threading;
using System;


namespace Candy.ViewModel
{
    public class HomeViewModel : ViewModelBase
    {

        private string _projectName;
        public string ProjectName
        {
            get
            {
                return _projectName;
            }
            set
            {
                _projectName = value;
                RaisePropertyChanged("ProjectName");
            }
        }


        INavigationService _navService;
        IDataService _dataService;
        public HomeViewModel(IDataService dataService, INavigationService navService)
        {
            _dataService = dataService;
            _navService = navService;
            _dataService.ProjectInitialized += _dataService_ProjectInitialized;



          
        }

        void _dataService_ProjectInitialized(object sender, EventArgs e)
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                ProjectName = _dataService.LoadedProject.Name;
                ProjectLoaded = true;
            });
        }


        private bool _projectLoaded;

        public bool ProjectLoaded
        {
            get { return _projectLoaded; }
            set {
                _projectLoaded = value;
                RaisePropertyChanged("ProjectLoaded");
                _navHome.RaiseCanExecuteChanged();
                _navigateSettings.RaiseCanExecuteChanged();
                _navBlast.RaiseCanExecuteChanged();
                _navSearch.RaiseCanExecuteChanged();
            }
        }
        private RelayCommand _navigateSettings;
        public RelayCommand NavigateSettings
        {
            get
            {
                return _navigateSettings ?? (_navigateSettings = new RelayCommand(ExecNavigateSettings, CanNavigateSettings)); 
            }
        }

        /// <summary>
        /// Checks whether the NavigateSettings command is executable
        /// </summary>
        private bool CanNavigateSettings()
        {
            return _projectLoaded;
        }

        /// <summary>
        /// Executes the NavigateSettings command 
        /// </summary>
        private void ExecNavigateSettings()
        {
            _navService.Navigate(NavigationTargets.Settings);
        }


        private RelayCommand _navTheme;
        public RelayCommand NavTheme
        {
            get
            {
                return _navTheme ?? (_navTheme = new RelayCommand(ExecNavTheme, CanNavTheme));
            }
        }

        /// <summary>
        /// Checks whether the NavTheme command is executable
        /// </summary>
        private bool CanNavTheme()
        {
            return true;
        }

        /// <summary>
        /// Executes the NavTheme command 
        /// </summary>
        private void ExecNavTheme()
        {
            _navService.Navigate(NavigationTargets.Theme);
        }


        private RelayCommand _navHome;
        public RelayCommand NavHome
        {
            get
            {
                return _navHome ?? (_navHome = new RelayCommand(ExecNavHome, CanNavHome));
            }
        }

        /// <summary>
        /// Checks whether the NavHome command is executable
        /// </summary>
        private bool CanNavHome()
        {
            return _projectLoaded;
        }

        /// <summary>
        /// Executes the NavHome command 
        /// </summary>
        private void ExecNavHome()
        {
            _navService.Navigate(NavigationTargets.Project);
        }


        private RelayCommand _navBlast;
        public RelayCommand NavBlast
        {
            get
            {
                return _navBlast ?? (_navBlast = new RelayCommand(ExecNavBlast, CanNavBlast));
            }
        }

        /// <summary>
        /// Checks whether the NavBlast command is executable
        /// </summary>
        private bool CanNavBlast()
        {
            return _projectLoaded;
        }

        /// <summary>
        /// Executes the NavBlast command 
        /// </summary>
        private void ExecNavBlast()
        {
            _navService.Navigate(NavigationTargets.JobManager);
        }


        private RelayCommand _navSearch;
        public RelayCommand NavSearch
        {
            get
            {
                return _navSearch ?? (_navSearch = new RelayCommand(ExecNavSearch, CanNavSearch));
            }
        }

        /// <summary>
        /// Checks whether the NavSearch command is executable
        /// </summary>
        private bool CanNavSearch()
        {
            return _projectLoaded;
        }

        /// <summary>
        /// Executes the NavSearch command 
        /// </summary>
        private void ExecNavSearch()
        {
            _navService.Navigate(NavigationTargets.Search);
        }


        private RelayCommand _initNavigation;
        public RelayCommand InitNavigation
        {
            get
            {
                return _initNavigation ?? (_initNavigation = new RelayCommand(ExecInitNavigation, CanInitNavigation));
            }
        }

        /// <summary>
        /// Checks whether the InitNavigation command is executable
        /// </summary>
        private bool CanInitNavigation()
        {
            return true;
        }

        /// <summary>
        /// Executes the InitNavigation command 
        /// </summary>
        private void ExecInitNavigation()
        {
            if (App.Current.IsRunningOutOfBrowser)
            {
                _navService.Navigate(NavigationTargets.Update);
            }
            else
            {
                _navService.Navigate(NavigationTargets.Install);
            }
        }

    }
}
